#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    init();//调用程序初始化函数,请勿在此函数前调用任何自定义函数
}

MainWindow::~MainWindow()
{
    delete ui;
}

//定时器m_timerWaitTest的超时响应槽函数
void MainWindow::on_timerWatiTest_timeOut()
{
    static int num=0;
    num++;
    if(num==1){
        ui->labWaitToTest->setText("正在等待发放试卷.");
    }else if(num==2){
        ui->labWaitToTest->setText("正在等待发放试卷..");
    }else if(num==3){
        ui->labWaitToTest->setText("正在等待发放试卷...");
        num=0;
    }

    //检测考试文件是否生成,如果已经生成则发送出相关信号
    QFile file(g_activeTestFilePath);
    if(file.exists()){
        emit testFileGenerated();
        m_timerWaitTest.stop();//停止考试文件生成检测定时器
    }
}

void MainWindow::on_timerArchieve_timeOut()
{
    //更新成绩表
    QFile file(g_archieveFilePath);
    if(file.open(QIODevice::ReadOnly)){
        m_archieveModel.removeRows(0,m_archieveModel.rowCount());
        file.readLine();

        for(;!file.atEnd();){
            QStringList list=QString::fromUtf8(file.readLine()).split('\t',Qt::SkipEmptyParts);
            if(list.count()==4){
                m_archieveModel.insertRow(m_archieveModel.rowCount());
                int row=m_archieveModel.rowCount()-1;

                m_archieveModel.setData(m_archieveModel.index(row,0),list.at(0));
                m_archieveModel.setData(m_archieveModel.index(row,1),list.at(1));
                m_archieveModel.setData(m_archieveModel.index(row,2),list.at(2));
                m_archieveModel.setData(m_archieveModel.index(row,3),QString::number(list.at(3).toDouble(),'f',2));
            }
        }
        file.close();
    }

}

void MainWindow::on_testFileGen()
{
    //跳转至学生信息确认界面
    QFile file(g_activeTestFilePath);
    if(file.open(QIODevice::ReadOnly)){
        file.readLine();
        QStringList list=QString::fromUtf8(file.readLine()).split('\t',Qt::SkipEmptyParts);
        if(list.count()==5){
            m_test.name=list.at(0);
            m_test.content=list.at(1);
            m_test.grade=list.at(2);
            m_test.desc=list.at(3);
            m_test.isOrdered=list.at(4);

            ui->labTestName->setText(list.at(0));
        }
    }
    ui->stackedWidget->setCurrentWidget(ui->pagStudentInfo);
}

void MainWindow::on_timerTestTime_timeOut()
{
    QTime time=QTime::fromString(ui->labTime->text(),"mm:ss");
    if(time.minute()==59&&time.second()==59){
        m_timerTestTime.stop();
        QMessageBox::warning(0,"警告","考试结束,自动交卷.");

        //保存最后一个选项
        for(auto i=m_exam.begin();i!=m_exam.end();++i){
            if(ui->labTopicExam->text()==i.value().topic){
                if(ui->radio1Exam->isChecked())i.value().choiceAnswer="1";
                else if(ui->radio2Exam->isChecked())i.value().choiceAnswer="2";
                else if(ui->radio3Exam->isChecked())i.value().choiceAnswer="3";
                else if(ui->radio4Exam->isChecked())i.value().choiceAnswer="4";
                break;
            }
        }

        //计算分数
        int rightCnt=0;
        for(auto i=m_exam.begin();i!=m_exam.end();++i){
            if(i.value().choiceAnswer==i.value().rightAnswer){
                ++rightCnt;
            }
        }

        QString core=QString::number(100/m_exam.count()*rightCnt,'f',2);

        //写入成绩
        QString costTime=QString("%1分%2秒").arg(QTime::fromString(ui->labTime->text(),"mm:ss").minute()).arg(QTime::fromString(ui->labTime->text(),"mm:ss").second());
        QFile file(g_archieveFilePath);
        if(file.open(QIODevice::WriteOnly|QIODevice::Append)){
            file.write("\r\n");
            file.write(m_student.name.toUtf8());
            file.write("\t");
            file.write(m_student.id.toUtf8());
            file.write("\t");

            file.write(costTime.toUtf8());
            file.write("\t");
            file.write(core.toUtf8());


            file.close();
        }

        ui->editNameEnd->setText(m_student.name);
        ui->editIdEnd->setText(m_student.id);
        ui->editCostTimeEnd->setText(costTime);
        ui->editCoreEnd->setText(core);

        ui->stackedWidget->setCurrentWidget(ui->pagEnd);

        return;
    }
    time=time.addSecs(1);
    ui->labTime->setText(time.toString("mm:ss"));

}

void MainWindow::on_topicBtnClicked()
{
    //获取信号发送则,转化为qpushbutton对象
    QPushButton* btn=static_cast<QPushButton*>(sender());

    //保存所选答案
    if(m_currentExam!=nullptr){
        if(ui->radio1Exam->isChecked())m_currentExam->choiceAnswer="1";
        else if(ui->radio2Exam->isChecked())m_currentExam->choiceAnswer="2";
        else if(ui->radio3Exam->isChecked())m_currentExam->choiceAnswer="3";
        else if(ui->radio4Exam->isChecked())m_currentExam->choiceAnswer="4";
    }

    //获取新题目指针
    m_currentExam=&m_exam[btn->text()];

    ui->labNumExam->setText("第"+btn->text()+"题");
    //加载所选答案
    ui->radio5->setChecked(true);

    ui->labTopicExam->setText(m_currentExam->topic);
    if(m_currentExam->choiceAnswer.toInt()==1)ui->radio1Exam->setChecked(true);
    else if(m_currentExam->choiceAnswer.toInt()==2)ui->radio2Exam->setChecked(true);
    else if(m_currentExam->choiceAnswer.toInt()==3)ui->radio3Exam->setChecked(true);
    else if(m_currentExam->choiceAnswer.toInt()==4)ui->radio4Exam->setChecked(true);

    //加载选项内容
    ui->lab1Exam->setText(m_currentExam->a1);
    ui->lab2Exam->setText(m_currentExam->a2);
    ui->lab3Exam->setText(m_currentExam->a3);
    ui->lab4Exam->setText(m_currentExam->a4);
}

//程序初始化函数,程序启动前最先调用的自定义函数
void MainWindow::init()
{
    //调整窗口大小
    this->resize(400,600);

    //隐藏登录界面身份选择框
    ui->groupBox_2->hide();

    //隐藏工具栏
    ui->toolBar->hide();
    ui->radio5->hide();

    ui->tvTopic->setEditTriggers(QAbstractItemView::NoEditTriggers);//默认题目表格不可编辑

    //初始化题目数据模型
    m_topicModel.setColumnCount(9);
    m_topicModel.setHeaderData(0,Qt::Horizontal,"题号");
    m_topicModel.setHeaderData(1,Qt::Horizontal,"题目");
    m_topicModel.setHeaderData(2,Qt::Horizontal,"答案1");
    m_topicModel.setHeaderData(3,Qt::Horizontal,"答案2");
    m_topicModel.setHeaderData(4,Qt::Horizontal,"答案3");
    m_topicModel.setHeaderData(5,Qt::Horizontal,"答案4");
    m_topicModel.setHeaderData(6,Qt::Horizontal,"正确答案");
    m_topicModel.setHeaderData(7,Qt::Horizontal,"难度");
    m_topicModel.setHeaderData(8,Qt::Horizontal,"是否有序");

    //读取题目信息
    QFile topicFile(g_topicFilePath);
    if(topicFile.open(QIODevice::ReadOnly)){
        topicFile.readLine();//读掉首行表头数据,使接下来读到的每一行都是题目数据
        for(;!topicFile.atEnd();){//读取每行数据并通过制表符划分
            QStringList _data=QString::fromUtf8(topicFile.readLine()).split('\t',Qt::SkipEmptyParts);
            if(_data.count()==9){//如果没有这行数据是完整的则写入到题目数据模型中
                m_topicModel.insertRow(m_topicModel.rowCount());
                int row=m_topicModel.rowCount()-1;
                m_topicModel.setData(m_topicModel.index(row,0),_data.at(0));
                m_topicModel.setData(m_topicModel.index(row,1),_data.at(1));
                m_topicModel.setData(m_topicModel.index(row,2),_data.at(2));
                m_topicModel.setData(m_topicModel.index(row,3),_data.at(3));
                m_topicModel.setData(m_topicModel.index(row,4),_data.at(4));
                m_topicModel.setData(m_topicModel.index(row,5),_data.at(5));
                m_topicModel.setData(m_topicModel.index(row,6),_data.at(6));
                m_topicModel.setData(m_topicModel.index(row,7),_data.at(7));
                m_topicModel.setData(m_topicModel.index(row,8),_data.at(8).toInt());
            }
        }
        topicFile.close();//关闭文件
    }
    QItemSelectionModel* selectMode=new QItemSelectionModel(&m_topicModel,this);//定义题目数据模型的选择模型
    ui->tvTopic->setModel(&m_topicModel);//绑定题目视图和模型
    ui->tvTopic->setSelectionModel(selectMode);//绑定题目数据模型和选择模型
    ui->tvTopic->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);//设置每列自动拉伸
    ui->tvTopic->setSelectionBehavior(QAbstractItemView::SelectRows);//设置题目浏览视图以行为单位选中
    // ui->tvTopic->setSelectionMode(QAbstractItemView::SingleSelection);//设置题目浏览视图只可以同时选中一行


    //读取试卷文件
    //初始化题目数据模型
    m_testModel.setColumnCount(9);
    m_testModel.setHeaderData(0,Qt::Horizontal,"题号");
    m_testModel.setHeaderData(1,Qt::Horizontal,"题目");
    m_testModel.setHeaderData(2,Qt::Horizontal,"答案1");
    m_testModel.setHeaderData(3,Qt::Horizontal,"答案2");
    m_testModel.setHeaderData(4,Qt::Horizontal,"答案3");
    m_testModel.setHeaderData(5,Qt::Horizontal,"答案4");
    m_testModel.setHeaderData(6,Qt::Horizontal,"正确答案");
    m_testModel.setHeaderData(7,Qt::Horizontal,"难度");
    m_testModel.setHeaderData(8,Qt::Horizontal,"是否有序");
    QFile testFile(g_testFilePath);
    if(testFile.open(QIODevice::ReadOnly)){
        testFile.readLine();//读掉首行表头数据,使接下来读到的每一行都是试卷数据
        for(;!testFile.atEnd();){//读取每行数据并通过制表符划分
            QStringList _data=QString::fromUtf8(testFile.readLine()).split('\t',Qt::SkipEmptyParts);
            if(_data.count()==5){//如果没有这行数据是完整的则写入到试卷数据模型中
                TEST t;
                t.name=_data.at(0);
                t.content=_data.at(1);
                t.grade=_data.at(2);
                t.desc=_data.at(3);
                t.isOrdered=_data.at(4);

                m_testMap[t.name]=t;
            }
        }
        testFile.close();//关闭文件
    }
    //更新试卷浏览界面的comb
    for(auto i=m_testMap.begin();i!=m_testMap.end();++i){
        ui->combTest->addItem(i.key());
    }
    if(ui->combTest->count()>0)emit ui->combTest->currentIndexChanged(ui->combTest->currentIndex());//发送信号,更新试卷题目表格
    ui->tvTest->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);//设置每列自动拉伸
    ui->tvTest->setSelectionBehavior(QAbstractItemView::SelectRows);//设置浏览视图以行为单位选中
    ui->tvTest->setSelectionMode(QAbstractItemView::SingleSelection);//设置浏览视图只可以同时选中一行
    ui->tvTest->setModel(&m_testModel);//绑定试卷视图和模型

    //初始化学生成绩模型
    m_archieveModel.setColumnCount(4);
    m_archieveModel.setHeaderData(0,Qt::Horizontal,"姓名");
    m_archieveModel.setHeaderData(1,Qt::Horizontal,"学号");
    m_archieveModel.setHeaderData(2,Qt::Horizontal,"考试用时");
    m_archieveModel.setHeaderData(3,Qt::Horizontal,"成绩");
    ui->tvArchieve->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);//设置每列自动拉伸
    ui->tvArchieve->setSelectionBehavior(QAbstractItemView::SelectRows);//设置浏览视图以行为单位选中
    ui->tvArchieve->setSelectionMode(QAbstractItemView::SingleSelection);//设置浏览视图只可以同时选中一行
    ui->tvArchieve->setModel(&m_archieveModel);//绑定试卷视图和模型

    m_timerWaitTest.setInterval(2000);//设置试卷发放检测时间周期为2000ms
    m_timerArchieve.setInterval(2000);//设置成绩数据模型更新时间周期为2000ms
    m_timerTestTime.setInterval(1000);

    //加载用户登录信息
    QFile _file(g_loginFilePath);//获取文件对象
    if(_file.open(QIODevice::ReadOnly)){//如果文件打开成功则读取文件信息到模型中
        _file.readLine();//读掉表头,使接下来每一行读到的都是数据
        for(;!_file.atEnd();){//写入表格数据
            QStringList _data= QString::fromUtf8(_file.readLine()).split('\t',Qt::SkipEmptyParts);//读取每行数据
            if(_data.size()<3)continue;//如果缺少信息则跳过该行数据

            //构建映射关系
            USER _u;
            _u.account=_data.at(0);
            _u.pswd=_data.at(1).toUtf8();
            _u.identity=(IDENTITY)(_data.at(2).toInt());
            m_userMap[_u.account]=_u;
        }
        _file.close();//关闭文件
    }/*else{//如果没有检测到用户数据文件则发出提示
        QMessageBox::warning(0,"警告","未检测到用户数据文件.");
    }*/

    //绑定信号和槽
    connect(&m_timerWaitTest,SIGNAL(timeout()),this,SLOT(on_timerWatiTest_timeOut()));
    connect(&m_timerArchieve,SIGNAL(timeout()),this,SLOT(on_timerArchieve_timeOut()));
    connect(this,SIGNAL(testFileGenerated()),this,SLOT(on_testFileGen()));
    connect(&m_timerTestTime,SIGNAL(timeout()),this,SLOT(on_timerTestTime_timeOut()));
}

//重写程序关闭函数,使程序关闭前先保存数据到文件
void MainWindow::closeEvent(QCloseEvent *e)
{
    //写入用户登录文件信息
    //检测文件目录是否存在,不存在则创建目录
    QStringList _list=QString(g_loginFilePath).split("/",Qt::SkipEmptyParts);
    if(_list.count()>=2){
        _list.removeAt(_list.count()-1);
        QDir dir(QDir::currentPath());
        if(!dir.exists(_list.join("/")))dir.mkdir(_list.join("/"));
    }
    //写入数据
    QFile file(g_loginFilePath);
    if(file.open(QIODevice::WriteOnly)){//如果文件打开成功则写入用户登录数据
        //写入表头
        file.write("account\t");
        file.write("pswd\t");
        file.write("identity");

        //写入数据
        for(auto i=m_userMap.begin();i!=m_userMap.end();++i){
            file.write("\r\n");

            file.write(i.value().account.toUtf8());
            file.write("\t");
            file.write(i.value().pswd);
            file.write("\t");
            file.write(QString::number(i.value().identity).toUtf8());
        }
    }
    file.close();//关闭文件


    //写入题目文件信息
    //检测文件目录是否存在,不存在则创建目录
    _list=QString(g_topicFilePath).split("/",Qt::SkipEmptyParts);
    if(_list.count()>=2){
        _list.removeAt(_list.count()-1);
        QDir dir(QDir::currentPath());
        if(!dir.exists(_list.join("/")))dir.mkdir(_list.join("/"));
    }
    //写入数据
    file.setFileName(g_topicFilePath);
    if(file.open(QIODevice::WriteOnly)){//如果文件打开成功则写入题目数据
        //写入表头
        file.write("num\t");
        file.write("topic\t");
        file.write("answer1\t");
        file.write("answer2\t");
        file.write("answer3\t");
        file.write("answer4\t");
        file.write("rightAnswer\t");
        file.write("grade\t");
        file.write("isOrdered");

        //写入数据
        for(int i=0;i<m_topicModel.rowCount();++i){
            file.write("\r\n");
            for(int j=0;j<m_topicModel.columnCount();++j){
                if(j==0)file.write(m_topicModel.data(m_topicModel.index(i,j)).toString().toUtf8());
                else{
                    file.write("\t");
                    file.write(m_topicModel.data(m_topicModel.index(i,j)).toString().toUtf8());
                }
            }
        }
    }
    file.close();//关闭文件


    //写入试卷文件信息
    //检测文件目录是否存在,不存在则创建目录
    _list=QString(g_testFilePath).split("/",Qt::SkipEmptyParts);
    if(_list.count()>=2){
        _list.removeAt(_list.count()-1);
        QDir dir(QDir::currentPath());
        if(!dir.exists(_list.join("/")))dir.mkdir(_list.join("/"));
    }
    //写入数据
    file.setFileName(g_testFilePath);
    if(file.open(QIODevice::WriteOnly)){//如果文件打开成功则写入题目数据
        //写入表头
        file.write("name\t");
        file.write("include\t");
        file.write("grade\t");
        file.write("knowledge\t");
        file.write("isOrdered");

        //写入数据
        for(auto i=m_testMap.begin();i!=m_testMap.end();++i){
            file.write("\r\n");
            file.write(i.value().name.toUtf8());
            file.write("\t");
            file.write(i.value().content.toUtf8());
            file.write("\t");
            file.write(i.value().grade.toUtf8());
            file.write("\t");
            file.write(i.value().desc.toUtf8());
            file.write("\t");
            file.write(i.value().isOrdered.toUtf8());
        }
    }

    file.close();//关闭文件

    //如果是作为教师身份退出程序则会清除正在测试的试卷文件和成绩文件
    if(ui->stackedWidget->currentWidget()==ui->pagTeacherManage){
        QFile::remove(g_activeTestFilePath);
        QFile::remove(g_archieveFilePath);
    }

    //关闭定时器
    m_timerArchieve.stop();

    e->accept();//关闭窗口
}

//当点击注册按钮时调用此函数
void MainWindow::on_btnSigUp_clicked()
{
    ui->stackedWidget->setCurrentWidget(ui->pagSigUp);//使程序跳转到注册界面
    this->resize(400,600);//调整界面尺寸
}

//当点击注册界面的确定按钮时调用此槽函数
void MainWindow::on_btnOkSigUP_clicked()
{
    //获取注册界面输入信息
    QString account=ui->editAccountSigUp->text();
    QString pswd=ui->editPswdSigUp->text();
    QString repswd=ui->editRePswdSigUp->text();

    //判断输入是否完整
    if(account.isEmpty()||pswd.isEmpty()||repswd.isEmpty()){//如果有没输入的信息则注册不成功
        QMessageBox::warning(0,"警告","输入信息不完整.");
        return;
    }

    //判断两次密码输入是否一致
    if(pswd!=repswd){//如果重新输入的密码和原密码不一样则注册不成功
        QMessageBox::warning(0,"警告","密码确认出现错误.");
        return;
    }

    //判断账号是否已经存在

    if(m_userMap.count(account)>0){
        QMessageBox::warning(0,"警告","该账号已存在.");
        return;
    }

    //验证通过
    //写入用户数据到模型中
    //密码加密
    QByteArray array;
    array.append(pswd.toUtf8());
    QCryptographicHash hash(QCryptographicHash::Md5); //Md5加密
    hash.addData(array); //添加数据
    QByteArray retArray = hash.result(); //加密后的数据

    USER _u;
    _u.account=account;
    _u.pswd=retArray;
    _u.identity=(IDENTITY)ui->combIdentitySigUp->currentIndex();
    m_userMap[account]=_u;

    //重置注册界面
    ui->editAccountSigUp->clear();
    ui->editPswdSigUp->clear();
    ui->editRePswdSigUp->clear();
    ui->combIdentitySigUp->setCurrentIndex(0);

    QMessageBox::information(0,"提示","注册成功!");//弹出提示框

    ui->stackedWidget->setCurrentWidget(ui->pagLogin);//返回登录界面
}

//当点击注册界面的取消按钮时调用此槽函数
void MainWindow::on_btnCancelSigUp_clicked()
{
    //重置注册界面
    ui->editAccountSigUp->clear();
    ui->editPswdSigUp->clear();
    ui->editRePswdSigUp->clear();
    ui->combIdentitySigUp->setCurrentIndex(0);

    ui->stackedWidget->setCurrentWidget(ui->pagLogin);//跳转到登录界面
}

//当点击登录界面的登录按钮时调用此槽函数
void MainWindow::on_btnLogin_clicked()
{
    QString account=ui->editAccount->text();
    QString pswd=ui->editPswd->text();

    if(account.isEmpty()||pswd.isEmpty()){
        QMessageBox::warning(0,"警告","输入信息不完整.");
        return;
    }

    QByteArray array;
    array.append(pswd.toUtf8());
    QCryptographicHash hash(QCryptographicHash::Md5); //Md5加密
    hash.addData(array); //添加数据
    QByteArray retArray = hash.result(); //加密后的数据

    //判断用户是否存在
    if(m_userMap.count(account)==0){//如果用户不存在
        QMessageBox::warning(0,"警告","用户不存在.");
        return;
    }else{//如果用户名存在

        if(QString(retArray).toUtf8()==m_userMap[account].pswd){//校验密码成功
            m_user=m_userMap[account];//更新用户实体
            if(m_user.identity==IDENTITY::STUDENT){//作为学生登录
                ui->stackedWidget->setCurrentWidget(ui->pagWaitToTest);
                m_timerWaitTest.start();//开始周期性检测考试文件是否生成


            }else if(m_user.identity==IDENTITY::TEACHER){//作为老师登录
                ui->stackedWidget->setCurrentWidget(ui->pagTeacherManage);
                this->resize(1000,600);//调整窗口尺寸
                this->move(QApplication::desktop()->width()/2-this->size().width()/2,QApplication::desktop()->height()/2-this->size().height()/2);//将窗口移动到屏幕中心

                ui->toolBar->show();//显示工具栏
                ui->stackTeacher->setCurrentWidget(ui->pagTopic);//默认为成绩展示界面
            }

        }else{//校验密码失败
            QMessageBox::warning(0,"警告","密码错误.");
            return;
        }
    }


}

//当actTopic触发时调用这个槽函数
void MainWindow::on_actTopic_triggered()
{
    ui->stackTeacher->setCurrentWidget(ui->pagTopic);//跳转至题目添加界面
}

//当点击教师题目管理界面的添加按钮时调用此函数
void MainWindow::on_btnAppendTopic_clicked()
{
    //重置添加界面的的输入框内容
    ui->spinTopicNumAP->setEnabled(true);
    ui->editTopicAP->clear();
    ui->editAnswer1AP->clear();
    ui->editAnswer2AP->clear();
    ui->editAnswer3AP->clear();
    ui->editAnswer4AP->clear();
    ui->cheAnswer1AP->setChecked(true);
    ui->combGradeAP->setCurrentIndex(0);
    ui->combIsOrderAP->setCurrentIndex(0);

    //添加题目时设置默认题号为最后一题的题号值加1,如果没有存在的题目则默认题目值从1开始
    int topicNum=1;
    if(m_topicModel.rowCount()>0){
        topicNum=m_topicModel.data(m_topicModel.index(m_topicModel.rowCount()-1,0)).toInt()+1;
    }
    ui->spinTopicNumAP->setValue(topicNum);
    ui->stackTeacher->setCurrentWidget(ui->pagAppendTopic);//跳转至题目浏览界面
}

//当编辑题目界面的取消按钮点击时调用此函数
void MainWindow::on_btnCancelAP_clicked()
{
    ui->tvTopic->setCurrentIndex(QModelIndex());//置空当前题目浏览表格的工作行
    ui->stackTeacher->setCurrentWidget(ui->pagTopic);//跳转至题目浏览界面
}

//当编辑题目界面的确定按钮点击时调用此函数
void MainWindow::on_btnOkAP_clicked()
{
    //获取题目的信息
    int topicNum=ui->spinTopicNumAP->value();
    QString header=ui->editTopicAP->toPlainText();
    QString awser1=ui->editAnswer1AP->toPlainText();
    QString awser2=ui->editAnswer2AP->toPlainText();
    QString awser3=ui->editAnswer3AP->toPlainText();
    QString awser4=ui->editAnswer4AP->toPlainText();
    int rightAnswers=-1;
    if(ui->cheAnswer1AP->isChecked())rightAnswers=1;
    else if(ui->cheAnswer2AP->isChecked())rightAnswers=2;
    else if(ui->cheAnswer3AP->isChecked())rightAnswers=3;
    else if(ui->cheAnswer4AP->isChecked())rightAnswers=4;
    int grade=ui->combGradeAP->currentIndex();
    int ordered=ui->combIsOrderAP->currentIndex();

    //如果题号spinbox可以编辑则当前处于添加题目模式
    if(ui->spinTopicNumAP->isEnabled()){
        //检验该题号是否存在

        for(int i=0;i<m_topicModel.rowCount();++i){
            if(m_topicModel.data(m_topicModel.index(i,0)).toInt()==topicNum){
                QMessageBox::warning(0,"警告","已存在该题号的题目.");
                return;
            }
        }

        //添加题目信息
        m_topicModel.insertRow(m_topicModel.rowCount());
        int row=m_topicModel.rowCount()-1;
        m_topicModel.setData(m_topicModel.index(row,0),topicNum);
        m_topicModel.setData(m_topicModel.index(row,1),header);

        if(awser1.isEmpty())m_topicModel.setData(m_topicModel.index(row,2),"无");
        else m_topicModel.setData(m_topicModel.index(row,2),awser1);
        if(awser2.isEmpty())m_topicModel.setData(m_topicModel.index(row,3),"无");
        else m_topicModel.setData(m_topicModel.index(row,3),awser2);
        if(awser3.isEmpty())m_topicModel.setData(m_topicModel.index(row,4),"无");
        else m_topicModel.setData(m_topicModel.index(row,4),awser3);
        if(awser4.isEmpty())m_topicModel.setData(m_topicModel.index(row,5),"无");
        else m_topicModel.setData(m_topicModel.index(row,5),awser4);

        m_topicModel.setData(m_topicModel.index(row,6),rightAnswers);
        m_topicModel.setData(m_topicModel.index(row,7),grade);
        m_topicModel.setData(m_topicModel.index(row,8),ordered);
    }else{//如果题目题号不可编辑则当前处于题目编辑模式
        int row=ui->tvTopic->currentIndex().row();//获取当前编辑的行
        m_topicModel.setData(m_topicModel.index(row,1),header);
        m_topicModel.setData(m_topicModel.index(row,2),awser1);
        m_topicModel.setData(m_topicModel.index(row,3),awser2);
        m_topicModel.setData(m_topicModel.index(row,4),awser3);
        m_topicModel.setData(m_topicModel.index(row,5),awser4);
        m_topicModel.setData(m_topicModel.index(row,6),rightAnswers);
        m_topicModel.setData(m_topicModel.index(row,7),grade);
        m_topicModel.setData(m_topicModel.index(row,8),ordered);
    }

    //返回教师题目浏览界面
    ui->stackTeacher->setCurrentWidget(ui->pagTopic);
}


void MainWindow::on_btnEditTopic_clicked()
{
    if(ui->tvTopic->currentIndex().isValid()){
        //重置添加界面的的输入框内容
        ui->spinTopicNumAP->setEnabled(false);//编辑题目是不可修改题目的题号
        int row=ui->tvTopic->currentIndex().row();//获取需要编辑的题目的行

        //填入题目信息
        ui->spinTopicNumAP->setValue(m_topicModel.data(m_topicModel.index(row,0)).toInt());
        ui->editTopicAP->setText(m_topicModel.data(m_topicModel.index(row,1)).toString());
        ui->editAnswer1AP->setText(m_topicModel.data(m_topicModel.index(row,2)).toString());
        ui->editAnswer2AP->setText(m_topicModel.data(m_topicModel.index(row,3)).toString());
        ui->editAnswer3AP->setText(m_topicModel.data(m_topicModel.index(row,4)).toString());
        ui->editAnswer4AP->setText(m_topicModel.data(m_topicModel.index(row,5)).toString());

        int rightAnswer=m_topicModel.data(m_topicModel.index(row,6)).toInt();
        if(rightAnswer==1)ui->cheAnswer1AP->setChecked(true);
        else if(rightAnswer==2)ui->cheAnswer2AP->setChecked(true);
        else if(rightAnswer==3)ui->cheAnswer3AP->setChecked(true);
        else if(rightAnswer==4)ui->cheAnswer4AP->setChecked(true);

        ui->combGradeAP->setCurrentIndex(m_topicModel.data(m_topicModel.index(row,7)).toInt());
        ui->combIsOrderAP->setCurrentIndex(m_topicModel.data(m_topicModel.index(row,8)).toInt());

        //跳转至题目编辑界面
        ui->stackTeacher->setCurrentWidget(ui->pagAppendTopic);
    }
}

//当教师题目浏览界面的删除按钮点击时调用此函数
void MainWindow::on_btnDeleteTopic_clicked()
{
    if(ui->tvTopic->currentIndex().isValid()){//检测当前行是否可用,可用则进行删除
        int res=QMessageBox::question(0,"提示","确定删除该题目?");//弹出确定删除对话框
        if(res==QMessageBox::Yes){//如果确定删除
            m_topicModel.removeRow(ui->tvTopic->currentIndex().row());//移除选中题目
            ui->tvTopic->setCurrentIndex(QModelIndex());//置空视图选中状态
        }
    }
}

//当修改题目浏览界面的难度筛选后调用此函数
void MainWindow::on_combGradeTopic_currentIndexChanged(int index)
{
    //index  0--所有难度  1--简单 2--普通 3--困难
    if(index==0){//显示所有难度的选择题
        for(int i=0;i<m_topicModel.rowCount();++i){
            ui->tvTopic->showRow(i);
        }
    }else{//显示指定难度的选择题
        for(int i=0;i<m_topicModel.rowCount();++i){
            ui->tvTopic->hideRow(i);
            if(m_topicModel.data(m_topicModel.index(i,7)).toInt()==index-1){
                ui->tvTopic->showRow(i);
            }
        }
    }
}

//点题目浏览界面的查询按钮点击时调用此函数
void MainWindow::on_btnFIndTopic_clicked()
{
    QString findTest=ui->editFindTopic->text();
    if(findTest.isEmpty()){//如果查找内容为空则执行一次和难度筛选一样的操作
        on_combGradeTopic_currentIndexChanged(ui->combGradeTopic->currentIndex());
    }else{//如果查找内容不为空

        //隐藏所有题目,仅显示符合要求的题目
        for(int i=0;i<m_topicModel.rowCount();++i)ui->tvTopic->hideRow(i);

        if(ui->combGradeTopic->currentIndex()==0){//如果当筛选的所有难度的题目
            for(int i=0;i<m_topicModel.rowCount();++i){
                //如果题号和查找内容相等或则题目中包含查找内容则显示这一题目
                if(m_topicModel.data(m_topicModel.index(i,0)).toInt()==findTest.toInt()||m_topicModel.data(m_topicModel.index(i,1)).toString().contains(findTest)){
                    ui->tvTopic->showRow(i);
                }
            }
        }else{//如果当前按难度对题目进行筛选
            for(int i=0;i<m_topicModel.rowCount();++i){
                //如果题号和查找内容相等或则题目中包含查找内容并且难度和筛选难度一样则显示这一题目
                if((m_topicModel.data(m_topicModel.index(i,0)).toInt()==findTest.toInt()||m_topicModel.data(m_topicModel.index(i,1)).toString().contains(findTest))&&
                    m_topicModel.data(m_topicModel.index(i,7)).toInt()==ui->combGradeTopic->currentIndex()-1){
                    ui->tvTopic->showRow(i);
                }
            }
        }
    }
}

//当点击界面上的试卷管理时调用此函数
void MainWindow::on_actTest_triggered()
{
    ui->stackTeacher->setCurrentWidget(ui->pagTest);//转到试卷管理界面
}

//当试题浏览界面的添加至试卷按钮点击时调用此函数
void MainWindow::on_btnAppendTest_clicked()
{
    //获取题目视图当前选中的行
    QModelIndexList list=ui->tvTopic->selectionModel()->selectedRows();

    if(list.count()!=0){//如果至少选中一行则跳转至试卷生成界面
        QString topicsStr;
        for(int i=0;i<list.count();++i){
            if(i==0)topicsStr+=m_topicModel.data(m_topicModel.index(list.at(i).row(),0)).toString();
            else topicsStr+=";"+m_topicModel.data(m_topicModel.index(list.at(i).row(),0)).toString();
        }

        //更新试卷编辑界面
        ui->editTestNameEdit->clear();
        ui->editContentEdit->setText(topicsStr);
        ui->spinCountEdit->setEnabled(false);
        ui->editContentEdit->setEnabled(false);
        ui->spinCountEdit->setValue(list.count());
        ui->combGradeEdit->setCurrentIndex(0);
        ui->labCoreEdit->setText(QString::number(100.0/list.count(),'f',2));
        ui->editDescEdit->clear();
        ui->combIsOrderedEdit->setCurrentIndex(0);

        ui->tvTopic->selectionModel()->clearSelection();

        ui->stackTeacher->setCurrentWidget(ui->pagEditTest);
    }
}

//当试卷编辑界面的取消按钮点击时调用此函数
void MainWindow::on_btnCancelEdit_clicked()
{
    //跳转至题目浏览界面
    ui->stackTeacher->setCurrentWidget(ui->pagTopic);
}

//当试卷编辑界面的确定按钮点击时调用此函数
void MainWindow::on_btnOkEdit_clicked()
{
    //将题目编辑界面的内容写入试卷图
    //检测是否存在同名试卷
    if(ui->editTestNameEdit->text().isEmpty()){
        QMessageBox::warning(0,"警告","试卷名不能为空.");
        return;
    }
    if(m_testMap.count(ui->editTestNameEdit->text())>0){
        QMessageBox::warning(0,"警告","已存在同名试卷.");
        return;
    }

    TEST t;
    t.name=ui->editTestNameEdit->text();
    t.content=ui->editContentEdit->toPlainText();
    t.grade=QString::number(ui->combGradeEdit->currentIndex());
    if(ui->editDescEdit->text().isEmpty()) t.desc="无";
    else t.desc=ui->editDescEdit->text();
    t.isOrdered=QString::number(ui->combIsOrderedEdit->currentIndex());

    m_testMap[t.name]=t;

    //更新试卷浏览界面的combTest
    ui->combTest->addItem(t.name);
    ui->stackTeacher->setCurrentWidget(ui->pagTest);
}

//当更改试卷浏览界面的combTest时调用此函数以更新试卷的题目表
void MainWindow::on_combTest_currentTextChanged(const QString &arg1)
{
    //更新试卷浏览界面信息
    TEST t=m_testMap[arg1];

    if(t.grade.toInt()==0)ui->labGradeTest->setText("简单");
    else if(t.grade.toInt()==1)ui->labGradeTest->setText("普通");
    else if(t.grade.toInt()==2)ui->labGradeTest->setText("困难");

    ui->labDescTest->setText(t.desc);

    if(t.isOrdered.toInt()==0)ui->labIsOrderedTest->setText("无序");
    else if(t.isOrdered.toInt()==1)ui->labIsOrderedTest->setText("无序");

    m_testModel.removeRows(0,m_testModel.rowCount());//移除原有表格数据
    QStringList topics=t.content.split(';',Qt::SkipEmptyParts);
    for(int i=0;i<topics.count();++i){
        for(int j=0;j<m_topicModel.rowCount();++j){
            if(m_topicModel.data(m_topicModel.index(j,0)).toInt()==topics.at(i).toInt()){
                m_testModel.insertRow(m_testModel.rowCount());
                int row=m_testModel.rowCount()-1;
                for(int k=0;k<m_testModel.columnCount();++k){
                    m_testModel.setData(m_testModel.index(row,k),m_topicModel.data(m_topicModel.index(j,k)));
                }
                break;
            }
        }
    }
}

//当试卷浏览界面的发布试卷按钮点击时调用此函数
void MainWindow::on_bntSendTest_clicked()
{
    if(ui->combTest->currentIndex()<0)return;

    int res=QMessageBox::question(0,"提示","确定要生成试卷: "+ui->combTest->currentText()+" 吗?");
    if(res!=QMessageBox::Yes)return;

    QStringList _list=QString(g_activeTestFilePath).split("/",Qt::SkipEmptyParts);
    if(_list.count()>=2){
        _list.removeAt(_list.count()-1);
        QDir dir(QDir::currentPath());
        if(!dir.exists(_list.join("/")))dir.mkdir(_list.join("/"));
    }

    _list=QString(g_archieveFilePath).split("/",Qt::SkipEmptyParts);
    if(_list.count()>=2){
        _list.removeAt(_list.count()-1);
        QDir dir(QDir::currentPath());
        if(!dir.exists(_list.join("/")))dir.mkdir(_list.join("/"));
    }
    QFile archieveFile(g_archieveFilePath);
    if(archieveFile.open(QIODevice::WriteOnly)){
        archieveFile.write("name\t");
        archieveFile.write("id\t");
        archieveFile.write("cost\t");
        archieveFile.write("archievement");
    }
    archieveFile.close();

    //生成试卷文件
    QFile file(g_activeTestFilePath);
    if(file.open(QIODevice::WriteOnly)){
        file.write("name\t");
        file.write("content\t");+
        file.write("grade\t");
        file.write("desc\t");
        file.write("isOrdered\r\n");

        TEST t=m_testMap[ui->combTest->currentText()];
        file.write(t.name.toUtf8());
        file.write("\t");
        file.write(t.content.toUtf8());
        file.write("\t");
        file.write(t.grade.toUtf8());
        file.write("\t");
        file.write(t.desc.toUtf8());
        file.write("\t");
        file.write(t.isOrdered.toUtf8());
        file.close();

        m_timerArchieve.start();
    }else{
        QMessageBox::warning(0,"警告","试卷生成失败.");
        return;
    }
}


void MainWindow::on_achAchieve_triggered()
{

    //检查测试试卷文件是否生成
    if(!QFile::exists(g_activeTestFilePath)){
        QMessageBox::warning(0,"警告","未发布试卷.");
        return;
    }

     //跳转至成绩管理界面
    ui->stackTeacher->setCurrentWidget(ui->pagAchieve);
}


void MainWindow::on_btnDeleteTest_clicked()
{
    //移除当前试卷
    if(ui->combTest->currentIndex()<0)return;
    int res=QMessageBox::question(0,"提示","确定要删除试卷: "+ui->combTest->currentText()+" 吗?");
    if(res==QMessageBox::Yes){
        m_testMap.remove(ui->combTest->currentText());
        ui->combTest->removeItem(ui->combTest->currentIndex());
    }
}


void MainWindow::on_btnStart_clicked()
{
    QString name=ui->editStudentName->text();
    QString id=ui->editStudentid->text();
    if(name.isEmpty()||id.isEmpty()){
        QMessageBox::warning(0,"警告","请输入完整的信息.");
        return;
    }

    if(!ui->cheReadAndConsent->isChecked()){
        QMessageBox::warning(0,"警告","请先同意考场规则.");
        return;
    }

    m_student.name=name;
    m_student.id=id;

    //更新题目数据表
    m_topicModel.removeRows(0,m_topicModel.rowCount());
    QFile topicFile(g_topicFilePath);
    if(topicFile.open(QIODevice::ReadOnly)){
        topicFile.readLine();//读掉首行表头数据,使接下来读到的每一行都是题目数据
        for(;!topicFile.atEnd();){//读取每行数据并通过制表符划分
            QStringList _data=QString::fromUtf8(topicFile.readLine()).split('\t',Qt::SkipEmptyParts);
            if(_data.count()==9){//如果没有这行数据是完整的则写入到题目数据模型中
                m_topicModel.insertRow(m_topicModel.rowCount());
                int row=m_topicModel.rowCount()-1;
                m_topicModel.setData(m_topicModel.index(row,0),_data.at(0));
                m_topicModel.setData(m_topicModel.index(row,1),_data.at(1));
                m_topicModel.setData(m_topicModel.index(row,2),_data.at(2));
                m_topicModel.setData(m_topicModel.index(row,3),_data.at(3));
                m_topicModel.setData(m_topicModel.index(row,4),_data.at(4));
                m_topicModel.setData(m_topicModel.index(row,5),_data.at(5));
                m_topicModel.setData(m_topicModel.index(row,6),_data.at(6));
                m_topicModel.setData(m_topicModel.index(row,7),_data.at(7));
                m_topicModel.setData(m_topicModel.index(row,8),_data.at(8).toInt());
            }
        }
        topicFile.close();//关闭文件
    }

    //生成题目
    QStringList topics=m_test.content.split(";",Qt::SkipEmptyParts);

    //生成题目集按钮
    QGridLayout* gLay=new QGridLayout();
    QPushButton* firstBtn=nullptr;
    for(int i=0;i<topics.count();++i){

        QPushButton* btn=new QPushButton(this);
        connect(btn,SIGNAL(clicked(bool)),this,SLOT(on_topicBtnClicked()));

        btn->setText(QString::number(i+1));

        if(firstBtn==nullptr)firstBtn=btn;

        gLay->addWidget(btn,i/3,i%3);

    }
    ui->groupTopicSet->setLayout(gLay);

    if(m_test.isOrdered.toInt()==0){//题目无序
        for(int i=1;topics.count()>0;++i){
            int index;
            if(topics.count()>1){
                index=QRandomGenerator(QTime::currentTime().msec()).bounded(topics.count());
            }else{
                index=0;
            }

            QString topNum=topics.at(index);

            EXAM e;
            e.id=topNum;

            for(int j=0;j<m_topicModel.rowCount();++j){
                if(m_topicModel.data(m_topicModel.index(j,0)).toInt()==topNum.toInt()){
                    e.topic=m_topicModel.data(m_topicModel.index(j,1)).toString();
                    e.isOrdered=m_topicModel.data(m_topicModel.index(j,8)).toString();


                    QString answer;
                    if(m_topicModel.data(m_topicModel.index(j,6)).toString().toInt()==1)answer=m_topicModel.data(m_topicModel.index(j,2)).toString();
                    else if(m_topicModel.data(m_topicModel.index(j,6)).toString().toInt()==2)answer=m_topicModel.data(m_topicModel.index(j,3)).toString();
                    else if(m_topicModel.data(m_topicModel.index(j,6)).toString().toInt()==3)answer=m_topicModel.data(m_topicModel.index(j,4)).toString();
                    else if(m_topicModel.data(m_topicModel.index(j,6)).toString().toInt()==4)answer=m_topicModel.data(m_topicModel.index(j,5)).toString();

                    if(e.isOrdered.toInt()==0){//选项无序
                        QStringList list;
                        list <<m_topicModel.data(m_topicModel.index(j,2)).toString()
                             <<m_topicModel.data(m_topicModel.index(j,3)).toString()
                             <<m_topicModel.data(m_topicModel.index(j,4)).toString()
                             <<m_topicModel.data(m_topicModel.index(j,5)).toString();
                        if(list.count()==4){
                            int index=QRandomGenerator(QTime::currentTime().msec()).bounded(4);
                            e.a1=list.at(index);;
                            list.removeAt(index);

                            index=QRandomGenerator(QTime::currentTime().msec()).bounded(3);
                            e.a2=list.at(index);
                            list.removeAt(index);

                            index=QRandomGenerator(QTime::currentTime().msec()).bounded(2);
                            e.a3=list.at(index);
                            list.removeAt(index);

                            e.a4=list.at(0);
                        }
                    }else{//选项有序
                        e.a1=m_topicModel.data(m_topicModel.index(j,2)).toString();
                        e.a2=m_topicModel.data(m_topicModel.index(j,3)).toString();
                        e.a3=m_topicModel.data(m_topicModel.index(j,4)).toString();
                        e.a4=m_topicModel.data(m_topicModel.index(j,5)).toString();
                    }

                    if(e.a1==answer)e.rightAnswer="1";
                    else if(e.a2==answer)e.rightAnswer="2";
                    else if(e.a3==answer)e.rightAnswer="3";
                    else if(e.a4==answer)e.rightAnswer="4";

                    break;
                }
            }

            m_exam[QString::number(i)]=e;
            topics.removeAt(index);
        }
    }else{//题目有序
        for(int i=0;i<topics.count();++i){
            EXAM e;

            for(int j=0;j<m_topicModel.rowCount();++j){
                if(m_topicModel.data(m_topicModel.index(j,0)).toInt()==topics.at(i).toInt()){
                    e.id=m_topicModel.data(m_topicModel.index(j,0)).toString();
                    e.topic=m_topicModel.data(m_topicModel.index(j,1)).toString();

                    e.isOrdered=m_topicModel.data(m_topicModel.index(j,8)).toString();

                    QString answer;
                    if(m_topicModel.data(m_topicModel.index(j,6)).toString().toInt()==1)answer=m_topicModel.data(m_topicModel.index(j,2)).toString();
                    else if(m_topicModel.data(m_topicModel.index(j,6)).toString().toInt()==2)answer=m_topicModel.data(m_topicModel.index(j,3)).toString();
                    else if(m_topicModel.data(m_topicModel.index(j,6)).toString().toInt()==3)answer=m_topicModel.data(m_topicModel.index(j,4)).toString();
                    else if(m_topicModel.data(m_topicModel.index(j,6)).toString().toInt()==4)answer=m_topicModel.data(m_topicModel.index(j,5)).toString();

                    if(e.isOrdered.toInt()==0){//选项无序
                        QStringList list;
                        list <<m_topicModel.data(m_topicModel.index(j,2)).toString()
                             <<m_topicModel.data(m_topicModel.index(j,3)).toString()
                             <<m_topicModel.data(m_topicModel.index(j,4)).toString()
                             <<m_topicModel.data(m_topicModel.index(j,5)).toString();
                        if(list.count()==4){
                            int index=QRandomGenerator(QTime::currentTime().msec()).bounded(4);
                            e.a1=list.at(index);;
                            list.removeAt(index);

                            index=QRandomGenerator(QTime::currentTime().msec()).bounded(3);
                            e.a2=list.at(index);
                            list.removeAt(index);

                            index=QRandomGenerator(QTime::currentTime().msec()).bounded(2);
                            e.a3=list.at(index);
                            list.removeAt(index);

                            e.a4=list.at(0);
                        }
                    }else{//选项有序
                        e.a1=m_topicModel.data(m_topicModel.index(j,2)).toString();
                        e.a2=m_topicModel.data(m_topicModel.index(j,3)).toString();
                        e.a3=m_topicModel.data(m_topicModel.index(j,4)).toString();
                        e.a4=m_topicModel.data(m_topicModel.index(j,5)).toString();
                    }

                    if(e.a1==answer)e.rightAnswer="1";
                    else if(e.a2==answer)e.rightAnswer="2";
                    else if(e.a3==answer)e.rightAnswer="3";
                    else if(e.a4==answer)e.rightAnswer="4";

                    break;
                }
            }

            m_exam[QString::number(i+1)]=e;
        }
    }

    //初始化考试时间计时系统
    ui->labTime->setText("00:00");
    m_timerTestTime.start();

    //加载第一题
    if(firstBtn!=nullptr)
    emit firstBtn->clicked();

    //跳转至考试界面
    ui->labNumExam->setText("第1题");
    ui->labStudentNameExam->setText(m_student.name);
    ui->labStudentIdExam->setText(m_student.id);
    ui->stackedWidget->setCurrentWidget(ui->pagExam);
}

//当点击交卷按钮时调用这个槽函数
void MainWindow::on_btnStopExan_clicked()
{
    int res=QMessageBox::question(0,"提示","确定要交卷吗?");
    if(res!=QMessageBox::Yes)return;

    m_timerTestTime.stop();//关闭考试计时器

    //保存最后一个选项
    for(auto i=m_exam.begin();i!=m_exam.end();++i){
        if(ui->labTopicExam->text()==i.value().topic){
            if(ui->radio1Exam->isChecked())i.value().choiceAnswer="1";
            else if(ui->radio2Exam->isChecked())i.value().choiceAnswer="2";
            else if(ui->radio3Exam->isChecked())i.value().choiceAnswer="3";
            else if(ui->radio4Exam->isChecked())i.value().choiceAnswer="4";
            break;
        }
    }

    //计算分数
    int rightCnt=0;
    for(auto i=m_exam.begin();i!=m_exam.end();++i){
        if(i.value().choiceAnswer==i.value().rightAnswer){
            ++rightCnt;
        }
    }

    QString core=QString::number(100.0/m_exam.count()*rightCnt,'f',2);

    //写入文件
    QString costTime=QString("%1分%2秒").arg(QTime::fromString(ui->labTime->text(),"mm:ss").minute()).arg(QTime::fromString(ui->labTime->text(),"mm:ss").second());
    QFile file(g_archieveFilePath);
    if(file.open(QIODevice::WriteOnly|QIODevice::Append)){
        file.write("\r\n");
        file.write(m_student.name.toUtf8());
        file.write("\t");
        file.write(m_student.id.toUtf8());
        file.write("\t");

        file.write(costTime.toUtf8());
        file.write("\t");
        file.write(core.toUtf8());

        file.close();
    }

    //跳转到结算界面
    ui->editNameEnd->setText(m_student.name);
    ui->editIdEnd->setText(m_student.id);
    ui->editCostTimeEnd->setText(costTime);
    ui->editCoreEnd->setText(core);

    ui->stackedWidget->setCurrentWidget(ui->pagEnd);
}


void MainWindow::on_btnOkEnd_clicked()
{
    this->close();
}

//当点击导出成绩到文件按钮点击时调用此函数
void MainWindow::on_btnGenFile_clicked()
{
    QString filePath=QFileDialog::getSaveFileName(0,"选择文件夹",QDir::homePath(),"文本文件(*.txt);;所有文件(*.*)");
    QFile file(filePath);
    if(file.open(QIODevice::WriteOnly)){
        file.write("name\t");
        file.write("id\t");
        file.write("costTime\t");
        file.write("archievement");
        for(int i=0;i<m_archieveModel.rowCount();++i){
            file.write("\r\n");
            for(int j=0;j<m_archieveModel.columnCount();++j){
                if(j==0)file.write(m_archieveModel.data(m_archieveModel.index(i,j)).toString().toUtf8());
                else{
                    file.write("\t");
                    file.write(m_archieveModel.data(m_archieveModel.index(i,j)).toString().toUtf8());
                }
            }
        }
        file.close();
        QMessageBox::information(0,"提示","文件已保存.");
    }
}

